<?php
class Message extends  Controller
{
    function Message()
    {
        parent::Controller();
      
		header("Content-Type: text/html; charset=utf-8");
		$this->userdata = checksession($this->session);
		
    }
    
    
    function index($page="")
    {
    	
        
    	  $page = $page==""?1:$page;
    	  $this->jypage->SetAttr(array(
			  'table'=>table("message")." as m left join ".table("users")." as u on m.userid=u.userid",
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'m.msgid',
			  'field'=>array("m.*","u.nickname"),
              'pageurl'=>templateurl."message/index",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStrJy();
    	
		 $this->tp->assign("pagelist",$pagelist["ZeroPage"]);
		 $this->tp->assign("pagestr",$pagestr);
    	$this->tp->assign("isuser",$this->userdata?1:0);
    	$this->tp->show("message");
    }
    function save()
    {
    	$result = array("error"=>1,"content"=>"");
    	if($this->userdata)
    	{
    		$message = $_POST["message"];
	    	$data = array(
				    	'userid'=>$this->userdata["userid"],
						'msg'=>$message
						    	);
			$this->db->insert('message',$data);
			
			
			$this->jypage->SetAttr(array(
			  'table'=>table("message")." as m left join ".table("users")." as u on m.userid=u.userid",
			  'pagesize'=>10,
			  'page'=>1,
			  'key'=>'m.msgid',
			  'field'=>array("m.*","u.nickname"),
              'pageurl'=>templateurl."message/index",
			  ));
			 $this->jypage->SetDb($this->db);
			
			 $pagelist = $this->jypage->pages();
			 $pagestr = $this->jypage->SetPageStrJy();
	    	
			 $this->tp->assign("pagelist",$pagelist["ZeroPage"]);
			 $this->tp->assign("pagestr",$pagestr);
			 
			 $result = array("error"=>0,"content"=>$this->tp->fetch("message_ajax.htm"));
			
			 
			
    	}
    	echo json_encode($result);
    				    	
    }
    
    
}    
?>